import { GSprite } from "../../../../com/mui/controls/GSprite";
import { ThingUtil } from "../../scene/layer/utils/ThingUtil";
import { SpritePlayer } from "../../scene/player/entity/SpritePlayer";
import { AIManager } from "../../scene/ai/AIManager";
import { AIType } from "../../scene/ai/AIType";
import { ObjCreate } from "../common/ObjCreate";
import { BaseProgressBar } from "../common/BaseProgressBar";
import { ImagesConst } from "../../resource/ImagesConst";
import { SPublicMiniPlayer } from "../../../../Message/Public/SPublicMiniPlayer";
import { PlayerUtil } from "../common/util/PlayerUtil";
import { CareerConfig } from "../../resource/CareerConfig";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../extend/language/Language";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { DataEvent } from "../../events/DataEvent";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { UserPlayer } from "../../scene/player/entity/UserPlayer";
import { PlayerEvent } from "../../scene/events/PlayerEvent";
import { IEntity } from "../../scene/player/IEntity";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { AttachedSoulBattlefieldUtil } from "./AttachedSoulBattlefieldUtil";
import { SEntityId } from "../../../../Message/Public/SEntityId";
import { EntityUtil } from "../../scene/layer/utils/EntityUtil";
import { PetPlayer } from "../../scene/player/entity/PetPlayer";
import { GBitmap } from "../../../../com/mui/controls/GBitmap";
import { GLabel } from "../../../../com/mui/controls/GLabel";
//class AttachedSoulBattlefielAidPlayer
    
    export  class AttachedSoulBattlefielAidPlayer extends GSprite
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */onClickUser(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=null;
            if (this._entityInfo) 
            {
                loc1 = ThingUtil.entityUtil.getEntity(this._entityInfo.entityId);
                if (loc1) 
                {
                    loc1.selected = true;
                    loc2 = ThingUtil.entityUtil.getEntity(this._miniPlayer.entityId) as SpritePlayer;
                    AIManager.onAIControl(loc2, AIType.AI_Entity);
                }
            }
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(0, 0, 198, 63, this, "ToolTipBg");
            this._userContainer = new Object /* flash.display.Sprite */();
            this._userContainer.x = 6;
            this._userContainer.y = 2;
            this.addChild(this._userContainer);
            this._userStateBitmap = new Object /* flash.display.Bitmap */();
            this._userStateBitmap.y = 2;
            this._userContainer.addChild(this._userStateBitmap);
            this._tfUserName = ObjCreate.createLabel("", 6, 6, 180, 20, flash.text.TextFormatAlign.LEFT, this._userContainer, true, 15859633);
            this._tfUserLevel = ObjCreate.createLabel("", 6, 23, 40, 20, flash.text.TextFormatAlign.LEFT, this._userContainer, true, 11661311);
            this._tfUserCareer = ObjCreate.createLabel("", 50, 23, 40, 20, flash.text.TextFormatAlign.LEFT, this._userContainer, true, 15788607);
            this._tfUserCombat = ObjCreate.createLabel("", 94, 23, 98, 20, flash.text.TextFormatAlign.LEFT, this._userContainer, true, 11661311);
            this._userProcessBar = new BaseProgressBar(ImagesConst.ProgressBarBg);
            this._userProcessBar.setSize(175, 14);
            this._userProcessBar.isScale = true;
            this._userProcessBar.progressBarClass = ImagesConst.EnergyBar;
            this._userProcessBar.setProgressPosition(8, 3);
            this._userProcessBar.setValue(0, 10000, this._progressBarPer);
            ObjCreate.setObjAttri(this._userProcessBar, 6, 40, -1, -1, this._userContainer);
            this._model = ObjCreate.createGBitmap("", 169, 5, this._userContainer);
            this._bmpState = ObjCreate.createGBitmap(ImagesConst.TeammateFlee, 150, 8, this);
            this._bmpState.visible = false;
            return;
        }

        public set miniPlayer(arg1: SPublicMiniPlayer)
        {
            this.dispose();
            this._miniPlayer = arg1;
            this._tfUserName.text = PlayerUtil.getProxyAndName(this._miniPlayer);
            this._tfUserLevel.htmlText = "LV." + this._miniPlayer.level;
            this._tfUserCareer.text = CareerConfig.instance.getInfoByCode(this._miniPlayer.careerEnable).name;
            this._tfUserCombat.htmlText = HTMLUtil.addColor(Language.getString(44594), "#B1efff") + HTMLUtil.addColor(this._miniPlayer.warfare.toString(), "#f0ea3f");
            Dispatcher.addEventListener(EventName.ArenaTeammateSteptIn, this.onGroupStepInHandler);
            Dispatcher.addEventListener(EventName.ArenaTeammateSteptOut, this.onGroupStepOutHandler);
            return;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this._miniPlayer = null;
            this._entityInfo = null;
            this._petEntityInfo = null;
            this._bmpState.visible = false;
            this._isDead = false;
            this.removeUserInfoListener();
            this._userPlayer = null;
            this._petPlayer = null;
            this.select = false;
            this._model.bitmapData = null;
            Dispatcher.removeEventListener(EventName.ArenaTeammateSteptIn, this.onGroupStepInHandler);
            Dispatcher.removeEventListener(EventName.ArenaTeammateSteptOut, this.onGroupStepOutHandler);
            return;
        }

        /* internal  */onGroupStepInHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityInfo;
            if (loc1) 
            {
                if (this.isSelf(loc1.entityId)) 
                {
                    this._bmpState.visible = false;
                    this.udpateInfo(loc1);
                    this.setLeave(false);
                    this.addUserInfoListener();
                }
            }
            return;
        }

        /* internal  */addUserInfoListener(): void
        {
            this.removeUserInfoListener();
            this._userPlayer = ThingUtil.entityUtil.getEntity(this._miniPlayer.entityId) as UserPlayer;
            if (this._userPlayer) 
            {
                this._userPlayer.addEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
            }
            return;
        }

        /* internal  */removeUserInfoListener(): void
        {
            if (this._userPlayer) 
            {
                this._userPlayer.removeEventListener(PlayerEvent.UPDATEINFO, this.onPlayerInfoUpdate);
            }
            return;
        }

        /* internal  */onPlayerInfoUpdate(arg1: PlayerEvent): void
        {
            var loc1=arg1.player as IEntity;
            this.udpateInfo(loc1.entityInfo.entityInfo);
            if (GameMapUtil.curMapState.isCrossSpiritWarCopyCode) 
            {
                this._model.bitmapData = AttachedSoulBattlefieldUtil.getBitmapDataByBuffers(loc1.entityInfo.entityInfo.buffers);
            }
            return;
        }

        /* internal  */onGroupStepOutHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            if (loc1) 
            {
                if (this.isSelf(loc1)) 
                {
                    this.setDead();
                    this.setLeave(true);
                    this._model.bitmapData = null;
                    this.removeUserInfoListener();
                }
            }
            return;
        }

        /* internal  */isSelf(arg1: SEntityId): Boolean
        {
            if (this._miniPlayer && arg1) 
            {
                return EntityUtil.equal(this._miniPlayer.entityId, arg1);
            }
            return false;
        }

        public isSelfEntity(arg1: string): Boolean
        {
            return EntityUtil.toString(this._miniPlayer.entityId) == arg1 || this._petEntityInfo && EntityUtil.toString(this._petEntityInfo.entityId) == arg1;
        }

        public udpateInfo(arg1: SEntityInfo): void
        {
            this._entityInfo = arg1;
            this._userProcessBar.setValue(arg1.life, arg1.maxLife, this._progressBarPer);
            return;
        }

        public set select(arg1: Boolean)
        {
            var loc1=null;
            this._userStateBitmap.visible = arg1;
            if (arg1) 
            {
                loc1 = new Object /* flash.display.BitmapData */(188, 54, true, 4279011482);
                this._userStateBitmap.bitmapData = loc1;
            }
            else 
            {
                this._userStateBitmap.bitmapData = null;
            }
            return;
        }

        public setDead(): void
        {
            this._isDead = true;
            var loc1=new Object /* flash.display.BitmapData */(188, 54, true, 4473924);
            this._userStateBitmap.bitmapData = loc1;
            this._userStateBitmap.visible = true;
            if (this._entityInfo) 
            {
                this._userProcessBar.setValue(0, this._entityInfo.maxLife, this._progressBarPer);
            }
            this._bmpState.visible = true;
            this.setCanClick(false);
            return;
        }

        public setLeave(arg1: Boolean): void
        {
            this.setCanClick(!arg1);
            return;
        }

        /* internal  */setCanClick(arg1: Boolean): void
        {
            if (arg1) 
            {
                this._userContainer.addEventListener(flash.events.MouseEvent.CLICK, this.onClickUser);
            }
            else 
            {
                this._userContainer.removeEventListener(flash.events.MouseEvent.CLICK, this.onClickUser);
            }
            return;
        }

        /* internal */ /* var */_miniPlayer: SPublicMiniPlayer;

        /* internal */ /* var */_entityInfo: SEntityInfo;

        /* internal */ /* var */_petEntityInfo: SEntityInfo;

        /* internal */ /* var */_userPlayer: UserPlayer;

        /* internal */ /* var */_petPlayer: PetPlayer;

        /* internal */ /* var */_progressBarPer: number=0.897142857143;

        /* internal */ /* var */_petProgressBarPer: number=0.866666666667;

        /* internal */ /* var */_isDead: Boolean=false;

        /* internal */ /* var */_bmpState: GBitmap;

        /* internal */ /* var */_userContainer: Object /* flash.display.Sprite */;

        /* internal */ /* var */_userStateBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_tfUserName: GLabel;

        /* internal */ /* var */_tfUserLevel: GLabel;

        /* internal */ /* var */_tfUserCareer: GLabel;

        /* internal */ /* var */_tfUserCombat: GLabel;

        /* internal */ /* var */_userProcessBar: BaseProgressBar;

        /* internal */ /* var */_model: Object /* flash.display.Bitmap */;
    }
